package utils

import "github.com/spf13/cast"

// Contains 函数用于判断切片是否包含指定元素
//
// 参数
//
//	-slice: 泛型切片
//	-e: 泛型对象
//
// 返回
//
//	-bool: 是/否
func SliceContains[T comparable](slice []T, e any) bool {
	if len(slice) == 0 || e == nil {
		return false
	}
	for _, v := range slice {
		if v == e {
			return true
		}
	}
	return false
}

// DeduplicateStrings 字符串切片去重
func DeduplicateStrings(list []string) []string {
	return Deduplicate(list, func(s string) string { return s })
}

// Deduplicate 切片去重
func Deduplicate[T any, K comparable](list []T, keyFunc func(T) K) []T {
	if len(list) <= 0 {
		return nil
	}
	seen := make(map[K]struct{})
	result := make([]T, 0, len(list))
	for _, item := range list {
		key := keyFunc(item)
		if _, exists := seen[key]; !exists {
			seen[key] = struct{}{}
			result = append(result, item)
		}
	}
	return result
}

func StringSliceToAnySlice(slice []string) []any {
	result := make([]any, 0, len(slice))
	for _, str := range slice {
		result = append(result, str)
	}
	return result
}

func ToAnySlice[T any](list []T) []any {
	if len(list) <= 0 {
		return nil
	}
	result := make([]any, len(list))
	for i, v := range list {
		result[i] = v
	}
	return result
}

func ToInt64Slice[T any](list []T) []int64 {
	if len(list) <= 0 {
		return nil
	}
	result := make([]int64, len(list))
	for i, v := range list {
		result[i] = cast.ToInt64(v)
	}
	return result
}